package com.qdu.web.controller.bussiness;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qdu.common.core.domain.model.LoginUser;
import com.qdu.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qdu.common.annotation.Log;
import com.qdu.common.core.controller.BaseController;
import com.qdu.common.core.domain.AjaxResult;
import com.qdu.common.enums.BusinessType;
import com.qdu.bussiness.domain.BusRent;
import com.qdu.bussiness.service.IBusRentService;
import com.qdu.common.utils.poi.ExcelUtil;
import com.qdu.common.core.page.TableDataInfo;

/**
 * 雪具租赁Controller
 * 
 * @author qdu
 * @date 2022-03-12
 */
@RestController
@RequestMapping("/bussiness/rent")
public class BusRentController extends BaseController
{
    @Autowired
    private IBusRentService busRentService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询雪具租赁列表
     */
    @PreAuthorize("@ss.hasPermi('bussiness:rent:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusRent busRent)
    {
        startPage();
        List<BusRent> list = busRentService.selectBusRentList(busRent);
        return getDataTable(list);
    }

    /**
     * 导出雪具租赁列表
     */
    @PreAuthorize("@ss.hasPermi('bussiness:rent:export')")
    @Log(title = "雪具租赁", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusRent busRent)
    {
        List<BusRent> list = busRentService.selectBusRentList(busRent);
        ExcelUtil<BusRent> util = new ExcelUtil<BusRent>(BusRent.class);
        util.exportExcel(response, list, "雪具租赁数据");
    }

    /**
     * 获取雪具租赁详细信息
     */
    @PreAuthorize("@ss.hasPermi('bussiness:rent:query')")
    @GetMapping(value = "/{rentId}")
    public AjaxResult getInfo(@PathVariable("rentId") Long rentId)
    {
        return AjaxResult.success(busRentService.selectBusRentByRentId(rentId));
    }

    /**
     * 新增雪具租赁
     */
    @PreAuthorize("@ss.hasPermi('bussiness:rent:add')")
    @Log(title = "雪具租赁", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusRent busRent)
    {
        AjaxResult ajaxResult = busRentService.insertBusRent(busRent);
        if(ajaxResult.get(AjaxResult.CODE_TAG).equals(200)){
            String bal = String.valueOf(ajaxResult.get(AjaxResult.DATA_TAG));
            LoginUser loginUser = getLoginUser();
            loginUser.getUser().setBalance(bal);
            tokenService.setLoginUser(loginUser);
        }
        return ajaxResult;
    }

    /**
     * 修改雪具租赁
     */
    @PreAuthorize("@ss.hasPermi('bussiness:rent:edit')")
    @Log(title = "雪具租赁", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusRent busRent)
    {
        return toAjax(busRentService.updateBusRent(busRent));
    }

    /**
     * 删除雪具租赁
     */
    @PreAuthorize("@ss.hasPermi('bussiness:rent:remove')")
    @Log(title = "雪具租赁", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rentIds}")
    public AjaxResult remove(@PathVariable Long[] rentIds)
    {
        return toAjax(busRentService.deleteBusRentByRentIds(rentIds));
    }
}
